package com.iexbuy.ihg.ui.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.iexbuy.ihg.App;
import com.iexbuy.ihg.R;
import com.iexbuy.ihg.adapter.LingQianLastestListAdapter;
import com.iexbuy.ihg.adapter.LingQianRecordHistoryListAdapter;
import com.iexbuy.ihg.common.ExtraKeyConstant;
import com.iexbuy.ihg.entity.LingQianLastestObject;
import com.iexbuy.ihg.entity.LingQianLastestObjectResponse;
import com.iexbuy.ihg.entity.LingQianRecordObject;
import com.iexbuy.ihg.entity.LingQianRecordObjectResponse;
import com.iexbuy.ihg.entity.LingQianSummaryObject;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.utils.JsonUtils;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class WeDeMianDanQueueCashFragment extends BaseFragment {

	private static WeDeMianDanQueueCashFragment homeFragmentInstent;

	@ViewInject(R.id.msgListLayout)
	private PullToRefreshLayout msgListRefreshLayout;
	@ViewInject(R.id.listView)
	private ListView listView;
	@ViewInject(R.id.totalCashNumberTv)
	private TextView totalCashNumberTv;
	@ViewInject(R.id.receivedUserNumberTv)
	private TextView receivedUserNumberTv;
	@ViewInject(R.id.attenceUesrNumber)
	private TextView attenceUesrNumber;

	@ViewInject(R.id.itemName)
	private TextView itemName;

	private LinearLayout listHeaderView;

	private String coinNumber;
	private String totalReceiveCash;
	private List<LingQianRecordObject> receivedRecords = new ArrayList<LingQianRecordObject>();
	private List<LingQianRecordObject> waitingRecords = new ArrayList<LingQianRecordObject>();
	private List<LingQianLastestObject>  lastRecords = new ArrayList<LingQianLastestObject>();
	private int pageNo = 0;
	private LingQianLastestListAdapter lingQianLastestListAdapter;

	private static LingQianSummaryObject summaryObject;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		homeFragmentInstent = this;
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_wede_maindan_records, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View rootView) {
		mBaseActivity.setTitle(getResources().getString(R.string.title_mian_dan));
		mBaseActivity.setTitleLayoutVisibility(true);
		mBaseActivity.setHomeActivity(false);

		listHeaderView = (LinearLayout) mBaseActivity.getLayoutInflater().from(mBaseActivity).inflate(R.layout.fragment_wede_miandan_queuecase_header, null);
		ViewUtils.inject(this, listHeaderView);
		listView.addHeaderView(listHeaderView);
		msgListRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				initData();
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				pullToRefreshLayout.refreshFinish(PullToRefreshLayout.DONE);
			}
		});

		initData();

	}


	public void initData() {
		HashMap<String, String> paramMap = new HashMap<String, String>();

		paramMap.put("pageNo", String.valueOf(pageNo));
		paramMap.put("pageSize","10");
		ServiceHelper.send(mBaseActivity, ServiceHelper.GET_LING_QIAN_LASTEST_RECORDS, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
					}

					@Override
					public void onCancelled() {
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.DONE);
						LingQianLastestObjectResponse rsp = JsonUtils.parseJsonStrToObject(responseInfo.result, LingQianLastestObjectResponse.class);

						if (pageNo == 0) {
							lastRecords.clear();
						}
						lastRecords.addAll(rsp.getBody());

						initListView();
					}

					@Override
					public void onFailure(HttpException error, String msg) {
					}
				});
	}

	private void initListView() {
		if (lastRecords!=null){
			itemName.setVisibility(View.GONE);
		}else {
			itemName.setVisibility(View.VISIBLE);
		}
		initHeaderView();
		if (lingQianLastestListAdapter == null) {
			lingQianLastestListAdapter = new LingQianLastestListAdapter(mBaseActivity, lastRecords);
			listView.setAdapter(lingQianLastestListAdapter);
		}
		lingQianLastestListAdapter.notifyDataSetChanged();
	}

	private void initHeaderView() {
		totalCashNumberTv.setText(summaryObject.getTotalCashNumber());
		receivedUserNumberTv.setText(summaryObject.getReceivedUserNumber());
		attenceUesrNumber.setText(summaryObject.getAttandUserNumber());
	}
	public static void setLingQianSummaryObject(LingQianSummaryObject summaryObject){
		WeDeMianDanQueueCashFragment.summaryObject = summaryObject;
	}
	/**
	 * @return the homeFragmentInstent
	 */
	public static WeDeMianDanQueueCashFragment getHomeFragmentInstent() {
		return homeFragmentInstent;
	}

}
